package com.alonsoruibal.chess.bitboard;

public class RookMagic {
	
	public final static byte rookShiftBits[] = {
		  12, 11, 11, 11, 11, 11, 11, 12,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  11, 10, 10, 10, 10, 10, 10, 11,
		  12, 11, 11, 11, 11, 11, 11, 12
		};
	
	// Magic numbers generated with MagicNumbersGen
	// there are 64 magic numbers
	public final static long rookMagicNumber[] = 
		{
		0x1080108000400020L, 0x40200010004000L,
		0x100082000441100L, 0x480041000080080L,
		0x100080005000210L, 0x100020801000400L,
		0x280010000800200L, 0x100008020420100L,
		0x400800080400020L, 0x401000402000L,
		0x100801000200080L, 0x801000800800L,
		0x800400080080L, 0x800200800400L,
		0x1000200040100L, 0x4840800041000080L,
		0x20008080004000L, 0x404010002000L, 
		0x808010002000L, 0x828010000800L,
		0x808004000800L, 0x14008002000480L,
		0x40002100801L, 0x20001004084L,
		0x802080004000L, 0x200080400080L,
		0x810001080200080L, 0x10008080080010L,
		0x4000080080040080L, 0x40080020080L,
		0x1000100040200L, 0x80008200004124L,
		0x804000800020L, 0x804000802000L,
		0x801000802000L, 0x2000801000800804L, 
		0x80080800400L, 0x80040080800200L, 
		0x800100800200L, 0x8042000104L,
		0x208040008008L, 0x10500020004000L,
		0x100020008080L, 0x2000100008008080L,
		0x200040008008080L, 0x8020004008080L,
		0x1000200010004L, 0x100040080420001L,
		0x80004000200040L, 0x200040100140L,
		0x20004800100040L, 0x100080080280L,
		0x8100800400080080L, 0x8004020080040080L, 
		0x9001000402000100L, 0x40080410200L, 
		0x208040110202L, 0x800810022004012L, 
		0x1000820004011L, 0x1002004100009L, 
		0x41001002480005L, 0x81000208040001L,
		0x4000008201100804L, 0x2841008402L
		};
	
	
	public static long[] rookMask;
	public static long[][] rookMagic;
	
	static {
		GenerateRookMagic.generateAttacks();
	}
	
	public static long getRookAttacks(int index, long all) {
		// You are inputting the square the rook stands on, and the position of all pieces,and getting 
		// back all the rook attacks; meaning empty squares and pieces that could be attacked
		
		int i = Gen.magicTransform(all & rookMask[index], rookMagicNumber[index], rookShiftBits[index]);
		return rookMagic[index][i];
	}
}
